package com.dbgs.executer.deliver;

import android.os.Handler;
import android.os.Looper;

import java.util.concurrent.Executor;

public class AndroidDeliver  implements Executor {


    private static volatile AndroidDeliver mInstance;

    private AndroidDeliver() {
    }

    public static AndroidDeliver getInstance() {
        if (mInstance == null) {
            synchronized (AndroidDeliver.class) {
                if (mInstance == null) {
                    mInstance = new AndroidDeliver();
                }
            }
        }
        return mInstance;
    }

    Handler mainHandler = new Handler(Looper.getMainLooper());


    @Override
    public void execute(final Runnable runnable) {
        //返回应用程序的looper，它位于应用程序的主线程中。
        Looper mainLooper = Looper.getMainLooper();
        if (mainLooper == Looper.myLooper()){
            runnable.run();
            return;
        }
        mainHandler.post(new Runnable() {
            @Override
            public void run() {
                //注意：这里需要增加非空判断
                if(runnable!=null){
                    runnable.run();
                }
            }
        });
    }
}
